<!DOCTYPE html>
<html>
<head>
    <title>Html5 Lesson 06 (Drag and drop)</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <style>
        header {
            background: #FFCC99;
            color: white;
            -moz-box-shadow: 0 2px 8px -3px rgba(0, 0, 0, .5), 0 1.4em 2em -0.7em rgba(255, 255, 255, .2) inset;
            -webkit-box-shadow: 0 2px 8px -3px rgba(0, 0, 0, .5), 0 1.4em 2em -0.7em rgba(255, 255, 255, .2) inset;
            box-shadow: 0 2px 8px -3px rgba(0, 0, 0, .5), 0 1.4em 2em -0.7em rgba(255, 255, 255, .2) inset;
            text-shadow: 1px 1px 1px #444;
        }

        header h1, header h2 {
            display: inline-block;
            padding: 12px 15px;
            font-size: 105%;
            line-height: 1;
            margin: 0;
        }

        header h1 {
            background: #FF9966;
        }

        .arrow-right {
            display: inline-block;
            width: 0;
            height: 0;
            border-top: 18px solid transparent;
            border-bottom: 18px solid transparent;
            border-left: 18px solid #FF9966;
            margin-bottom: -11px;
        }
        #drag-zone {
            list-style: none;
            float: left;
        }

        #drag-zone li * {
            border: 4px solid #888;
            display: block;
            margin: 10px 0;
        }

        #drag-zone, #drop-zone, #drop-data {
            width: 20%;
        }

        #drop-zone, #drop-data {
            padding: 40px;
            border: 5px solid #888;
            float: right;
            height: 240px;
            overflow: auto;
        }

        #drop-zone.hovering {
            border: 5px solid #aaa;
            background-color: rgba(255, 0, 0, 0.199219);
        }

        #drop-data {
            background-color: #eee;
            font-family: Monospace;
            -ms-word-wrap: break-word;
            word-wrap: break-word;
        }

        .datatypes {
            font-weight: bold;
        }

        .draggable-text {
            padding: 5px;
        }
    </style>
</head>
<body>
<header>
    <h1>抓放</h1>
    <div class="arrow-right"></div>
    <h2>原生浏览器支持抓放</h2>
</header>
<div id="dnd-example">
    <ol id="drag-zone">
        <li><img src="http://derekliu.blog.51cto.com/album/4479510/133601724534.png" draggable class="copy"/></li>
        <li>
            <span class="draggable-text" draggable>选择文字然后拖放到目的区</span>
        </li>
    </ol>
    <div id="drop-data">Source Data</div>
    <div id="drop-zone">Drop Area</div>
</div>
<script>
    var dragZone = document.querySelector('#drag-zone');
    var dropZone = document.querySelector('#drop-zone');
    dragZone.addEventListener('dragstart', function (event) {
        if (event.target.className) { // img case
            event.dataTransfer.effectAllowed = event.target.className;
            event.target.style.border = "4px solid #cc3300";
        } else { // text case
            event.target.parentNode.style.border = "4px solid #cc3300";
        }
        return true;
    }, true);

    dragZone.addEventListener('dragend', function (event) {
        if (event.target.className) { // img case
            event.target.style.border = "4px solid #888";
        } else { // text case
            event.target.parentNode.style.border = "4px solid #888";
        }
        return true;
    }, true);

    dropZone.addEventListener('dragenter', function (event) {
        if (event.preventDefault) event.preventDefault();
        event.dataTransfer.dropEffect = 'copy';
        this.className = 'hovering';
        return false;
    }, false);

    dropZone.addEventListener('dragover', function (event) {
        if (event.preventDefault) event.preventDefault(); // allows us to drop
        event.dataTransfer.dropEffect = 'copy';
        return false;
    }, false);

    dropZone.addEventListener('dragleave', function (event) {
        if (event.preventDefault) event.preventDefault(); // allows us to drop
        this.className = '';
        return false;
    }, false);

    dropZone.addEventListener('drop', function (event) {
        if (event.preventDefault) event.preventDefault();
        var imgPassed = null;
        var dropdata = document.querySelector('#drop-data');
        var types = event.dataTransfer.types;
        document.querySelector('#drop-data').textContent = '';
        this.innerHTML = '';
        for (var i = 0; i < types.length; i++) {
            if (types[i] == 'Files') {
                var files = event.dataTransfer.files;
                for (var j = 0; j < files.length; j++) {
                    dropdata.textContent += 'File Name: ' + files[j].fileName;
                    dropdata.textContent += 'File Size: ' + files[j].fileSize;
                }
            } else {
                if (typeof event.dataTransfer.getData(types[i]) !== 'undefined') {
                    dropdata.innerHTML += '<p><em class="datatypes">' + types[i] + '</em>: <br />' + event.dataTransfer.getData(types[i]).replace(/</g, '&lt;') + '</p>';
                }
            }

            if (types[i] == 'text/uri-list') {
                imgPassed = event.dataTransfer.getData('text/uri-list');
            }
        }

        if (imgPassed) {
            var cEl = document.createElement('canvas');
            cEl.width = 200;
            cEl.height = 100;
            var ctx = cEl.getContext('2d');
            var img_buffer = document.createElement('img');
            img_buffer.src = imgPassed;
            img_buffer.style.display = 'none';
            document.body.appendChild(img_buffer); // this line only needed in safari
            img_buffer.onload = function () {
                ctx.drawImage(img_buffer, 0, 0, 100, 100);
            }
            this.appendChild(cEl);
        } else {
            if (event.dataTransfer.getData('text')) {
                this.innerHTML = event.dataTransfer.getData('text');
            } else if (event.dataTransfer.getData('text/plain')) {
                this.innerHTML = event.dataTransfer.getData('text/plain');
            }
        }
        return false;
    }, false);
</script>
</body>
</html>​